﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class AddTokenVm
    Inherits KeyedViewModelBase

    Private mObj As RawObject
    Private mInsertIndex As Integer

    Public Sub New()

    End Sub

    Public Sub New(obj As RawObject, insertIndex As Integer)
        mObj = obj
        mInsertIndex = insertIndex

        Tokens = New ObservableCollection(Of TokenInfo)

        For Each tokenName As String In TokenDictionary.Instance.GetTokensFor(obj.ObjectType, obj.ObjectSubType)
            Dim def As TokenDefinition = TokenDictionary.Instance.GetDefinition(tokenName)
            Dim usage As TokenUsage = def.Usage(obj.ObjectType, obj.ObjectSubType)
            Tokens.Add(New TokenInfo() With {.Name = tokenName, .Description = usage.Description})
        Next

        FilteredTokens = CollectionViewSource.GetDefaultView(Tokens)
        FilteredTokens.Filter = Function(item As Object) As Boolean
                                    If String.IsNullOrEmpty(FilterText) Then
                                        Return True
                                    Else
                                        Dim info As TokenInfo = CType(item, TokenInfo)
                                        Return info.Name.ToUpperInvariant.Contains(FilterText.ToUpperInvariant) OrElse info.Description.ToUpperInvariant.Contains(FilterText.ToUpperInvariant)
                                    End If
                                End Function
    End Sub

    Private mClearFilterCommand As ICommand = New Command(Sub() FilterText = "")
    Public ReadOnly Property ClearFilterCommand As ICommand
        Get
            Return mClearFilterCommand
        End Get
    End Property

    Private mAddTokenCommand As ICommand = New Command(AddressOf AddToken)
    Public ReadOnly Property AddTokenCommand As ICommand
        Get
            Return mAddTokenCommand
        End Get
    End Property


    Private mCancelCommand As ICommand = New Command(Sub() SendMessage(New Messages.Common.CloseWindow))
    Public ReadOnly Property CancelCommand As ICommand
        Get
            Return mCancelCommand
        End Get
    End Property

    Private Sub AddToken(param As Object)
        'mObj.Tokens.Insert(mInsertIndex, New Token("ANewToken", New List(Of String)))
        'ObjectChangeManager.Instance.NotifyObjectChanged(mObj, True, Me)
        SendMessage(New Messages.Common.CloseDialog(True))
    End Sub

    Private mFilterText As String
    Public Property FilterText As String
        Get
            Return mFilterText
        End Get
        Set(value As String)
            If mFilterText <> value Then
                mFilterText = value
                RaisePropertyChanged("FilterText")
                CollectionViewSource.GetDefaultView(Tokens).Refresh()
            End If
        End Set
    End Property

    Public Property Tokens As ObservableCollection(Of TokenInfo)

    Private mFilteredTokens As ICollectionView
    Public Property FilteredTokens As ICollectionView
        Get
            Return mFilteredTokens
        End Get
        Set(value As ICollectionView)
            If mFilteredTokens IsNot value Then
                mFilteredTokens = value
                RaisePropertyChanged("FilteredTokens")
            End If
        End Set
    End Property

    Public Class TokenInfo
        Public Property Name As String
        Public Property Description As String
    End Class
End Class
